package logic

import (
	"context"
	"strconv"

	"demo/internal/errorx"
	"demo/internal/svc"
	"demo/internal/types"
	"demo/model"

	"github.com/zeromicro/go-zero/core/logx"
	"github.com/zeromicro/go-zero/core/stores/redis"
)

type ServeyDeleteLogic struct {
	logx.Logger
	rds *redis.Redis
	ctx    context.Context
	svcCtx *svc.ServiceContext
	modelS model.ServeyModel
	modelSSC model.SevSevClfModel
	modelO model.OptionModel
	modelQ model.QuestionModel
}

const(
	deleted = iota
	no_delete
)

func NewServeyDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ServeyDeleteLogic {
	rds, err := redis.NewRedis(redis.RedisConf{
		Host: svcCtx.Config.Redis.Host,
		Pass: svcCtx.Config.Redis.Pass,
		Type: svcCtx.Config.Redis.Type,
	})
	if err != nil {
		logx.Errorf("redis init error: %v", err)
	}
	return &ServeyDeleteLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		rds: rds,
		svcCtx: svcCtx,
		modelS: model.NewServeyModel(svcCtx.Orm,svcCtx.Config.Cache),
		modelSSC: model.NewSevSevClfModel(svcCtx.Orm),
		modelO: model.NewOptionModel(svcCtx.Orm),
		modelQ: model.NewQuestionModel(svcCtx.Orm),
	}
}

func (l *ServeyDeleteLogic) ServeyDelete(req *types.IdRequest) (resp *types.CommonResponse, err error) {
	// todo: add your logic here and delete this line
	l.rds.DelCtx(l.ctx,ServeyDetailPrefix + strconv.FormatInt(req.Id,10))
	// 软删除问卷
	servey,err := l.modelS.FindOne(l.ctx,nil,req.Id)
	if err != nil {
		return nil,errorx.NewBizError(500,err.Error())
	}
	servey.IsDeleted = deleted
	err = l.modelS.Update(l.ctx,nil,servey)
	if err != nil {
		return nil,errorx.NewBizError(500,err.Error())
	}
	return &types.CommonResponse{
		Code: 200,
		Msg: "delete success",
	},nil
}
